﻿#include "confirm.h"

Confirm::Confirm(QWidget * parent)
	: QDialog(parent) {
	ui.setupUi(this);
	setWindowFlags(Qt::Dialog | Qt::FramelessWindowHint);

	m_isPressed = false;
}

Confirm::~Confirm() {
}

void Confirm::setText(QString text) {
	ui.lblText->setText(text);
}

void Confirm::on_pbSure_clicked() {
	Q_EMIT confirmOKSignal();
	close();
}

void Confirm::on_pbCancel_clicked() {
	close();
}

void Confirm::on_pbClose_clicked() {
	close();
}

void Confirm::mousePressEvent(QMouseEvent* event) {
	if (event->button() == Qt::LeftButton
		&& ui.widTitle->geometry().contains(event->pos())) {
		m_isPressed = true;
		m_startMovePos = event->globalPos();
	}
}

void Confirm::mouseMoveEvent(QMouseEvent* event) {
	if (m_isPressed) {
		QPoint movePoint = event->globalPos() - m_startMovePos;
		QPoint pos = this->pos() + movePoint;
		m_startMovePos = event->globalPos();
		this->move(pos.x(), pos.y());
	}
}

void Confirm::mouseReleaseEvent(QMouseEvent* event) {
	m_isPressed = false;
}

void Confirm::keyPressEvent(QKeyEvent *event) {
	switch (event->key()) {
	case Qt::Key_Escape:
		break;
	default:
		QDialog::keyPressEvent(event);
	}
}

void Confirm::paintEvent(QPaintEvent *event) {
	QPainterPath path;
	path.setFillRule(Qt::WindingFill);
	path.addRect(5, 5, this->width() - 10, this->height() - 10);

	QPainter painter(this);
	painter.setRenderHint(QPainter::Antialiasing, true);
	painter.fillPath(path, QBrush(Qt::white));

	QColor color(0, 0, 0, 50);
	for (int i = 0; i < 10; i++) {
		QPainterPath path;
		path.setFillRule(Qt::WindingFill);
		path.addRect(5 - i, 5 - i, this->width() - (5 - i) * 2, this->height() - (5 - i) * 2);
		color.setAlpha(100 - qSqrt(i) * 50);
		painter.setPen(color);
		painter.drawPath(path);
	}
}
